package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.CstInfo;
import com.fx.service.CstInfoService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yfl
 * @email 617823298@qq.com
 * @date 2018-06-19 16:25:20
 */

@Controller
@RequestMapping("/cstInfo")
public class CstInfoController extends BaseController {
    @Autowired
    private CstInfoService cstInfoService;
    private static String pageTitle = "客户管理";
    private static String pageUrl = "/cstInfo";

    @GetMapping()
    String cstInfo(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        CstInfo cstInfo = new CstInfo();
        return "cstInfo/cstInfo";
    }

    @ResponseBody
    @GetMapping("/list")
    public JSONObject list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<CstInfo> cstInfoList = cstInfoService.getList(query);
        int total = cstInfoService.getCount(query);
        return renderPage(total, cstInfoList);
    }

    @GetMapping("/add")
    String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "cstInfo/add";
    }

    @GetMapping("/edit/{flId}")
    String edit(@PathVariable("flId") String flId, Model model) {
        CstInfo cstInfo = cstInfoService.getById(flId);
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        model.addAttribute("cstInfo", cstInfo);
        return "cstInfo/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(CstInfo cstInfo) {
        String id = cstInfo.getFlId();
        int result = 0;
        if (id == null || "".equals(id)) {
            result = cstInfoService.save(cstInfo);
        } else {
            result = cstInfoService.updateById(cstInfo);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDel")
    @ResponseBody
    public R remove(@RequestParam("ids") String[] ids) {
        if (cstInfoService.batchRemove(ids) != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    @RequestMapping("/initCombo")
    @ResponseBody
    public List<CstInfo> initCombo(){
        Map<String,Object> param = new HashMap<String, Object>();
        List<CstInfo> cstInfoList = new ArrayList<CstInfo>();
        CstInfo cstInfo = new CstInfo();
        cstInfo.setFlId("-1");
        cstInfo.setFlName("平台");
        cstInfoList.add(cstInfo);
        cstInfoList.addAll(cstInfoService.getList(param));
        return cstInfoList;
    }

}
